import sys
import traceback
import time
from event_bus import EventBus

def test_with_polling():
    """测试带轮询的完整流程"""
    print("Starting test with polling...")
    try:
        bus = EventBus()
        print("EventBus created")
        
        # 注册一个简单的处理器
        def handler(payload):
            print(f"Received payload: {payload}")
        
        bus.on(0xC0, handler)
        print("Handler registered")
        
        # 发送几个事件
        for i in range(3):
            bus.emit(0xC0, f"test_payload_{i}")
            print(f"Sent event {i}")
            
        # 等待一段时间让事件处理
        print("Waiting for events to be processed...")
        time.sleep(2)
        
        bus.shutdown()
        print("Test completed successfully")
        return True
    except Exception as e:
        print(f"Error occurred: {e}")
        traceback.print_exc()
        return False

if __name__ == "__main__":
    print("Running test with polling...")
    success = test_with_polling()
    if success:
        print("All tests passed")
        sys.exit(0)
    else:
        print("Tests failed")
        sys.exit(1)